package com.secondkill.controller;

import com.secondkill.entity.MiaoshaOrder;
import com.secondkill.entity.MiaoshaUser;
import com.secondkill.entity.OrderInfo;
import com.secondkill.messagequeue.rabbitmq.MiaoshaMessage;
import com.secondkill.messagequeue.rabbitmq.RabbitMqSender;
import com.secondkill.redis.MiaoShaGoodsKey;
import com.secondkill.redis.RedisService;
import com.secondkill.result.CodeMsg;
import com.secondkill.result.Result;
import com.secondkill.service.MiaoshaGoodsService;
import com.secondkill.service.MiaoshaService;
import com.secondkill.service.MiaoshaUserService;
import com.secondkill.service.OrderService;
import com.secondkill.vo.GoodsVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IBM on 2018/7/11.
 */
@Controller
@RequestMapping("/miaosha")
public class MiaoShaController implements InitializingBean{

    @Autowired
    MiaoshaGoodsService goodsService;

    @Autowired
    OrderService orderService;

    @Autowired
    MiaoshaService miaoshaService;

    @Autowired
    RedisService redisService;


    @Autowired
    RabbitMqSender sender;
    @Override
    public void afterPropertiesSet() throws Exception {
        // 加载库存信息到redis中
        List<GoodsVo> goodsVos = goodsService.listGoodsVo();

        goodsVos.stream().forEach(goodsVo -> {
            redisService.set(MiaoShaGoodsKey.miaoshaGoodsStock, "" + goodsVo.getId(), goodsVo.getStockCount());
        });

    }

    @PostMapping("/do_miaosha")
    @ResponseBody
    public Result<Integer> doMiaoSha(Model model, MiaoshaUser user,
                          @RequestParam("goodsId")long goodsId) {

        model.addAttribute("user", user);
        if(user == null) {
            return Result.error(CodeMsg.SESSION_ERROR);
        }
        // yujian预减redis 库存
        //预减库存
        long stock = redisService.decr(MiaoShaGoodsKey.miaoshaGoodsStock, ""+goodsId);//10
        if(stock < 0) {
            //localOverMap.put(goodsId, true);
            return Result.error(CodeMsg.MIAO_SHA_OVER);
        }
        //判断是否已经秒杀到了
        MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId);
        if(order != null) {
            return Result.error(CodeMsg.REPEATE_MIAOSHA);
        }
        //入队
        MiaoshaMessage mm = new MiaoshaMessage();
        mm.setMiaoshaUser(user);
        mm.setGoodsId(goodsId);
        sender.sendMiaoshaMessage(mm);
        return Result.success(0);//排队中
        /*GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
        int stock = goods.getStockCount();
        if(stock <= 0) {
            model.addAttribute("errmsg", CodeMsg.MIAO_SHA_OVER.getMsg());
            return "miaosha_fail";
        }
        //判断是否已经秒杀到了
        MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId);
        if(order != null) {
            model.addAttribute("errmsg", CodeMsg.REPEATE_MIAOSHA.getMsg());
            return "miaosha_fail";
        }
        //减库存 下订单 写入秒杀订单
        OrderInfo orderInfo = miaoshaService.miaosha(user, goods);
        model.addAttribute("orderInfo", orderInfo);
        model.addAttribute("goods", goods);
        return "order_detail";*/

    }
}
